package exam.axhu.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import exam.axhu.entity.Choice;
import exam.axhu.service.ChoiceService;


@SuppressWarnings("serial")
@Controller @Scope("prototype")
public class ChoiceAction extends ActionSupport{
//defaultClass: choiceLogin
	@Resource ChoiceService choiceService;

	private Choice choice;
	public Choice getChoice() {
		return choice;
	}
	public void setChoice(Choice choice) {
		this.choice = choice;
	}
	
	
	public String editChoiceA(){
		ActionContext.getContext().put("editchoice", choiceService.findByID(choice.getChoiceID()));
		return "Edit";
	}
	

	public String editChoiceB(){
		Choice newchoice = choiceService.findByID(choice.getChoiceID());
		newchoice.setChoiceA(choice.getChoiceA());
		newchoice.setChoiceB(choice.getChoiceB());
		newchoice.setChoiceC(choice.getChoiceC());
		newchoice.setChoiceD(choice.getChoiceD());
		newchoice.setChoiceContent(choice.getChoiceContent());
		newchoice.setChoiceAnswer(choice.getChoiceAnswer());
		choiceService.update(newchoice);
		addActionMessage("修改成功!欢迎!");
		ActionContext.getContext().put("choices", choiceService.list());
		return "EditSucceed";
	}
	
	public String editChoices(){
		ActionContext.getContext().put("choices", choiceService.list());
		return "ChoiceInfo";
	}

	
	public String AddChoiceA(){
		return "AddChoice";
	}
	
	public String AddChoiceB(){
	    
		if(choice.getChoiceA().length()!=0 &&choice.getChoiceB().length()!=0 &&choice.getChoiceC().length()!=0 &&choice.getChoiceD().length()!=0 &&choice.getChoiceContent().length()!=0 &&choice.getChoiceAnswer().length()!=0)
		{
			
			choiceService.save(choice);
			addActionMessage("添加成功!欢迎!");
			ActionContext.getContext().put("choices", choiceService.list());
			return "AddSucceed";
		}
				
		addActionError("题目内容、选项、答案不能为空!");
		return "AddFailed";
	}

	public String DeleteChoiceA(){
		ActionContext.getContext().put("deleteChoice", choiceService.findByID(choice.getChoiceID()));
		return "DeleteChoice";
	}
	
	public String DeleteChoiceB(){
		if(choice!=null)
			choiceService.delete(choice.getChoiceID());
		ActionContext.getContext().put("choices", choiceService.list());
		return "DeleteSucceed";
	}
	
}
